package com.noahplat.user.mapper;

import com.noahplat.user.pojo.Permission;
import com.noahplat.user.utils.PermissionAdminDao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 权限关系映射
 *
 * @author 李东
 * @version 1.0.0
 * @Date: 2021/2/27 9:02
 **/
public interface PermissionMapper extends Mapper<Permission> {
    @Insert("<script> INSERT INTO tb_permission(stid,acctid,permission,description,created_by,created_time) VALUES <foreach collection = 'list' item='record' separator=','>" +
            "(#{record.stid},#{record.acctid},#{record.permission},#{record.description},#{record.createdBy},#{record.createdTime})</foreach></script>")
    int insertPermission(@Param("list") List<Permission> list);

    @Select("select id from tb_permission where stid=#{stid} and acctid=#{acctid}")
    Set<Long> getPid(@Param("stid") Long stid,@Param("acctid") Long acctid);

    @Select("select permission as permission,description as description from tb_permission where id in (select max(id) from tb_permission group by permission)")
    List<PermissionAdminDao> getPermission();

    @Select("select p.id,p.stid,p.acctid,p.permission,p.description,p.created_by as createdBy," +
            "p.created_time as createdTime,t.sys_name as sysname,a.acctname as accountname from tb_permission p left join sys_type t on t.id = p.stid " +
            " left join tb_account a on a.id = p.acctid")
    List<Permission> selectPermission();

    @Select("select p.id,p.stid,p.acctid,p.permission,p.description,p.created_by as createdBy," +
            "p.created_time as createdTime,t.sys_name as sysname,a.acctname as accountname from tb_permission p left join sys_type t on t.id = p.stid " +
            " left join tb_account a on a.id = p.acctid where p.stid=#{stid}")
    List<Permission> selectPermissionByStid(@Param("stid") Long stid);
}
